﻿using AutoMapper;
using FluentValidation;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    [ApiController]
    [Route("/api/v1")]
    public class ProductionUnitController : ControllerBase
    {
        private readonly ILogger<ProductionUnitController> _logger;
        private readonly IProductionUnitService _productionUnitService;
        private readonly IMapper _mapper;
        private readonly IValidator<ProductionUnitInput> _productionUnitInputValidator;
        private readonly IValidator<ProductionUnitUpdateInput> _productionUnitUpdateInputValidator;
        private readonly IValidator<ProductionUnitPageInput> _productionUnitPageInputValidator;
        private readonly IValidator<ProductionUnitChildsInput> _productionUnitChildsInputValidator;

        public ProductionUnitController(ILogger<ProductionUnitController> logger,
            IProductionUnitService productionUnitService,
            IMapper mapper,
            IValidator<ProductionUnitInput> productionUnitInputValidator,
            IValidator<ProductionUnitUpdateInput> productionUnitUpdateInputValidator,
            IValidator<ProductionUnitPageInput> productionUnitPageInputValidator,
            IValidator<ProductionUnitChildsInput> productionUnitChildsInputValidator
            )
        {
            _logger = logger;
            _productionUnitService = productionUnitService;
            _mapper = mapper;
            _productionUnitInputValidator = productionUnitInputValidator;
            _productionUnitUpdateInputValidator = productionUnitUpdateInputValidator;
            _productionUnitPageInputValidator = productionUnitPageInputValidator;
            _productionUnitChildsInputValidator = productionUnitChildsInputValidator;
        }

        /// <summary>
        /// 新增生产单元
        /// </summary>
        /// <param name="productionUnitInput"></param>
        /// <returns></returns>
        [HttpPost("productionunits")]
        public async Task<ActionResult> AddProductionUnit(ProductionUnitInput productionUnitInput)
        {
            try
            {
                var input = await _productionUnitInputValidator.ValidateAsync(productionUnitInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _productionUnitService.AddProductionUnitAsync(productionUnitInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 修改生产单元
        /// </summary>
        /// <param name="productionUnitUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("productionunits")]
        public async Task<ActionResult> UpdateProductionUnit(ProductionUnitUpdateInput productionUnitUpdateInput)
        {
            try
            {
                var input = await _productionUnitUpdateInputValidator.ValidateAsync(productionUnitUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _productionUnitService.UpdateProductionUnitAsync(productionUnitUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除生产单元
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("productionunits")]
        public async Task<ActionResult> DeleteProductionUnit(int[] ids)
        {
            try
            {
                await _productionUnitService.DeleteProductionUnitAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询生产单元
        /// </summary>
        /// <param name="productionUnitTypePageInput"></param>
        /// <returns>PagedItemsData<ProductionUnitResult></returns>
        [HttpGet("productionunits")]
        public async Task<ActionResult> GetProductionUnits([FromQuery] ProductionUnitPageInput productionUnitPageInput)
        {
            try
            {
                var input = await _productionUnitPageInputValidator.ValidateAsync(productionUnitPageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _productionUnitService.GetProductionUnitAsync(productionUnitPageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号查询生产单元
        /// </summary>
        /// <param name="id"></param>
        /// <returns>ProductionUnitResult</returns>
        [HttpGet("productionunits/entity")]
        public async Task<ActionResult> GetProductionUnitEntityById(int id)
        {
            try
            {
                var result = await _productionUnitService.GetProductionUnitAsyncById(id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 获取整棵树
        /// </summary>
        /// <returns>json树</returns>
        [HttpGet("productionunits/tree")]
        public async Task<ActionResult> GetProductionUnitTree()
        {
            try
            {
                var result = await _productionUnitService.GetProductionUnitTreeAsync();
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 获取所有孩子结点
        /// </summary>
        /// <param name="productionUnitChildsInput"></param>
        /// <returns></returns>
        [HttpGet("productionunits/childs")]
        public async Task<ActionResult> GetProductionUnitChildTree([FromQuery] ProductionUnitChildsInput productionUnitChildsInput)
        {
            try
            {
                var input = await _productionUnitChildsInputValidator.ValidateAsync(productionUnitChildsInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _productionUnitService.GetProductionUnitChildsAsync(productionUnitChildsInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
